package com.example.project7;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class InterestSelectActivity extends AppCompatActivity {
    private CheckBox cbReading;
    private CheckBox cbPhotography;
    private CheckBox cbPingpong;
    private Button btnBack;
    private Button btnConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest_select);

        // 初始化控件
        cbReading = findViewById(R.id.cb_reading);
        cbPhotography = findViewById(R.id.cb_photography);
        cbPingpong = findViewById(R.id.cb_pingpong);
        btnBack = findViewById(R.id.btn_back);
        btnConfirm = findViewById(R.id.btn_confirm);

        // 返回按钮点击事件
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 确定按钮点击事件
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取选中的兴趣
                StringBuilder interestBuilder = new StringBuilder();
                if (cbReading.isChecked()) {
                    if (interestBuilder.length() > 0) interestBuilder.append(", ");
                    interestBuilder.append(cbReading.getText().toString());
                }
                if (cbPhotography.isChecked()) {
                    if (interestBuilder.length() > 0) interestBuilder.append(", ");
                    interestBuilder.append(cbPhotography.getText().toString());
                }
                if (cbPingpong.isChecked()) {
                    if (interestBuilder.length() > 0) interestBuilder.append(", ");
                    interestBuilder.append(cbPingpong.getText().toString());
                }

                String interest = interestBuilder.toString();
                if (interest.isEmpty()) {
                    interest = "未选择";
                }

                // 返回选择的兴趣
                Intent intent = new Intent();
                intent.putExtra("interest", interest);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}
